/*
 *  Copyright 2015-2016, RenGu Technology (BeiJing) Co., Ltd.
 *  All right reserved.
 *
 *  THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF RENGU TECHNOLOGY (BeiJing) CO., LTD.
 *
 *  THE CONTENTS OF THIS FILE MAY NOT BE DISCLOSED TO THIR PARTIES,
 *  COPIED OR DUPLICATED IN ANY FORM, IN WHOLE OR IN PART,
 *  WITHOUT THE PRIOR WRITTEN PERMISSION OF RENGU TECHNOLOGY (BeiJing) CO., LTD.
 *
 *  AcctDayCutInfoMapperImpl
 *
 *  Edit History:
 *      lizhangtao Created on 2020年04月13日
 *
 */
package cc.rengu.igas.acct.common.dao.impl;

import cc.rengu.igas.acct.common.constant.AcctSqlMapperId;
import cc.rengu.igas.acct.common.dao.AcctDayCutInfoMapper;
import cc.rengu.igas.acct.common.dao.MapperCommon;
import cc.rengu.igas.acct.common.entity.AcctDaycutInfo;
import cc.rengu.igas.acct.common.enums.AcctRespCodeEnum;
import cc.rengu.igas.acct.common.enums.CutDayStatusEnum;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.utility.base.StringUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.dbs.Database;

/**
 * 日切表操作实现
 *
 * @author lizhangtao
 * @version 1.0
 * @date 2020/5/8
 */
public class AcctDayCutInfoMapperImpl extends MapperCommon implements AcctDayCutInfoMapper {

    /**
     * 查询机构的日切信息
     *
     * @param instId 法人机构号
     * @return 成功返回机构日切信息，未查询到信息返回null
     * @throws BizException 其他失败及异常都抛出异常
     */
    @Override
    public AcctDaycutInfo selectAcctDayCutInfoByInstId(String instId) throws BizException {
        try {
            extractDbPoolName();
            DbsUtil dbsUtil = new DbsUtil(dbPoolName);
            AcctDaycutInfo acctDaycutInfo = new AcctDaycutInfo();
            acctDaycutInfo.setInstId(instId);
            acctDaycutInfo.setDaycutStatus(CutDayStatusEnum.END.getCutDayStatus());
            String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE,
                    AcctSqlMapperId.SELECT_ACCT_DAY_CUT_BY_INST_ID);
            int retCode = dbsUtil.dbsExecuteSql(sqlStr, acctDaycutInfo, acctDaycutInfo);
            if (Database.DBS_SUCCESS == retCode) {
                return acctDaycutInfo;
            } else if (Database.DBS_NOT_FOUND == retCode) {
                return null;
            } else {
                rglog.error("查询失败(非未查询到数据)");
                throw new BizException(AcctRespCodeEnum.DB_ERROR.getRespCode(), AcctRespCodeEnum.DB_ERROR.getRespDesc());
            }
        } catch (BizException e) {
            throw e;
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            throw new BizException(AcctRespCodeEnum.DB_EXCEPTION.getRespCode(), AcctRespCodeEnum.DB_EXCEPTION.getRespDesc());
        }
    }

    /**
     * 查询机构的日切信息
     *
     * @param instId  法人机构号
     * @param dbsUtil 数据库句柄
     * @return 成功返回机构日切信息，未查询到信息返回null
     * @throws BizException 其他失败及异常都抛出异常
     */
    @Override
    public AcctDaycutInfo selectAcctDayCutInfoByInstId(String instId, DbsUtil dbsUtil) throws BizException {
        try {
            AcctDaycutInfo acctDaycutInfo = new AcctDaycutInfo();
            acctDaycutInfo.setInstId(instId);
            acctDaycutInfo.setDaycutStatus(CutDayStatusEnum.END.getCutDayStatus());
            String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE,
                    AcctSqlMapperId.SELECT_ACCT_DAY_CUT_BY_INST_ID);
            int retCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, acctDaycutInfo, acctDaycutInfo);
            if (Database.DBS_SUCCESS == retCode) {
                return acctDaycutInfo;
            } else if (Database.DBS_NOT_FOUND == retCode) {
                return null;
            } else {
                rglog.error("查询失败(非未查询到数据)");
                throw new BizException(AcctRespCodeEnum.DB_ERROR.getRespCode(), AcctRespCodeEnum.DB_ERROR.getRespDesc());
            }
        } catch (BizException e) {
            throw e;
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            throw new BizException(AcctRespCodeEnum.DB_EXCEPTION.getRespCode(), AcctRespCodeEnum.DB_EXCEPTION.getRespDesc());
        }
    }

    /**
     * 锁定日切表机构数据
     *
     * @param instId 法人机构号
     * @return 成功返回账户当前记账日期信息
     * @throws BizException 无数据、其他失败、异常
     */
    @Override
    public AcctDaycutInfo lockByInstId(String instId) throws BizException {
        try {
            extractDbPoolName();
            DbsUtil dbsUtil = new DbsUtil(dbPoolName);
            AcctDaycutInfo acctDaycutInfo = new AcctDaycutInfo();
            acctDaycutInfo.setInstId(instId);
            acctDaycutInfo.setDaycutStatus(CutDayStatusEnum.END.getCutDayStatus());
            String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE,
                    AcctSqlMapperId.LOCK_ACCT_DAY_CUT_BY_INST_ID);
            int retCode = dbsUtil.dbsExecuteSql(sqlStr, acctDaycutInfo, acctDaycutInfo);
            if (Database.DBS_SUCCESS == retCode) {
                return acctDaycutInfo;
            } else if (Database.DBS_NOT_FOUND == retCode) {
                rglog.error("锁定机构<{}>日切失败(未查询到数据):{}", instId, dbsUtil.getDbInst().dbsGetError());
                throw new BizException(AcctRespCodeEnum.INST_ACCT_DAY_CFG_ERROR.getRespCode(), AcctRespCodeEnum.INST_ACCT_DAY_CFG_ERROR.getRespDesc());
            } else {
                rglog.error("锁定机构<{}>日切失败(非未查询到数据):{}", instId, dbsUtil.getDbInst().dbsGetError());
                throw new BizException(AcctRespCodeEnum.DB_ERROR.getRespCode(), AcctRespCodeEnum.DB_ERROR.getRespDesc());
            }
        } catch (BizException e) {
            throw e;
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            throw new BizException(AcctRespCodeEnum.DB_EXCEPTION.getRespCode(), AcctRespCodeEnum.DB_EXCEPTION.getRespDesc());
        }
    }
}
